var dictionary={
    data:{list:[]}
}

dictionary.Show=function(){
    msgbox.Loading();
    m.request({
        method:"GET",
        url:"php/dictionary.php?p=list"
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            dictionary.data.list=res.list;
            m.render(document.querySelector(".content"),m(dictionary))
        }else{
            msgbox.Error(res.message);
        }
    })
}

dictionary.view=function(){
    return [
        m("div.page_title","数字字典"),
        m("div.page_menu",[
            m("button",{onclick:dictionary_add.Show},"新增")
        ]),
        m("div.page_content.page_content2",m("table",[
            m("tr",[
                m("th","中文"),
                m("th","英文"),
                m("th","正则"),
                m("th","操作")
            ]),
            dictionary.data.list.map(item=>{
                if(item.i_drop==1) return;
                return m("tr",[
                    m("td",item.s_cn),
                    m("td",item.s_en),
                    m("td",item.s_regex),
                    m("td",[
                        m("button",{onclick:function(){
                            dictionary_edit.Show(item)
                        }},"更新"),
                        m("button",{onclick:function(){
                            dictionary.Delete(item)
                        }},"删除")
                    ])
                ])
            })
        ]))
    ]
}

var dictionary_add={
    regex:[
        {"key":"s_cn","regex":/^.{1,300}$/,"desc":"中文"},
        {"key":"s_en","regex":/^.{1,300}$/,"desc":"英文"},
        {"key":"s_regex","regex":/^.{1,300}$/,"desc":"正则"},
    ]
}

dictionary_add.Show=function(){
    m.render(document.querySelector(".content"),m(dictionary_add));
}

dictionary_add.view=function(){
    return [
        m("div.page_title","【新增】数据字典"),
        m("div.page_menu",[
            m("button",{onclick:dictionary.Show},"返回")
        ]),
        m("div.page_content",[
            m("div.page_line",[
                m("div.page_tag","中文"),
                m("input[type=text].page_input#s_cn")
            ]),
            m("div.page_line_br"),
            m("div.page_line",[
                m("div.page_tag","英文"),
                m("input[type=text].page_input#s_en")
            ]),
            m("div.page_line_br"),
            m("div.page_line",[
                m("div.page_tag","正则"),
                m("input[type=text].page_input#s_regex")
            ])
        ]),
        m("div.page_foot",[
            m("button",{onclick:dictionary_add.Submit},"保存")
        ])
    ]
}

dictionary_add.Submit=function(){
    var postData=index.GetPostData(dictionary_add.regex);
    if(!postData){
        msgbox.Error("输入有误，请检查");
        return;
    }
    var _that=this;
    _that.disable=true;
    msgbox.Loading();
    m.request({
        method:"POST",
        url:"php/dictionary.php?p=add",
        body:postData
    }).then(function(res){
        msgbox.Hide();
        _that.disable=false;
        if(res.res=="OK"){
            dictionary.Show();
        }else{
            msgbox.Error(res.message);
        }
    })
}

var dictionary_edit={
    data:{}
}

dictionary_edit.Show=function(item){
    dictionary_edit.data=item;
    m.render(document.querySelector(".content"),m(dictionary_edit));
}

dictionary_edit.view=function(){
    return [
        m("div.page_title","【更新】数据字典"),
        m("div.page_menu",[
            m("button",{onclick:dictionary.Show},"返回")
        ]),
        m("div.page_content",[
            m("div.page_line",[
                m("div.page_tag","中文"),
                m("input[type=text].page_input#s_cn",{value:dictionary_edit.data.s_cn})
            ]),
            m("div.page_line_br"),
            m("div.page_line",[
                m("div.page_tag","英文"),
                m("input[type=text].page_input#s_en",{value:dictionary_edit.data.s_en})
            ]),
            m("div.page_line_br"),
            m("div.page_line",[
                m("div.page_tag","正则"),
                m("input[type=text].page_input#s_regex",{value:dictionary_edit.data.s_regex})
            ])
        ]),
        m("div.page_foot",[
            m("button",{onclick:dictionary_edit.Submit},"保存")
        ])
    ]
}

dictionary_edit.Submit=function(){
    var postData=index.GetPostData(dictionary_add.regex);
    if(!postData){
        msgbox.Error("输入有误，请检查");
        return;
    }
    postData.append("id",dictionary_edit.data.id);
    var _that=this;
    _that.disable=true;
    msgbox.Loading();
    m.request({
        method:"POST",
        url:"php/dictionary.php?p=edit",
        body:postData
    }).then(function(res){
        msgbox.Hide();
        _that.disable=false;
        if(res.res=="OK"){
            dictionary.Show();
        }else{
            msgbox.Error(res.message);
        }
    })
}